package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    // dubbo消费者
    @Reference
    UserService userService;

    @Reference
    MenuService menuService;


    // 从SpringSecurity中获取用户信息
    @RequestMapping(value = "/findUsername")
    public Result findUsername() {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            List<Menu> menuList = menuService.findMenuByUsername(user.getUsername());
            Map<String, Object> map = new HashMap<>();


            List<Map> listMap = new ArrayList<>();

            for (Menu menu : menuList) {
                Map<String, Object> menuMap = new HashMap<>();
                menuMap.put("path", menu.getPath());
                menuMap.put("title", menu.getName());
                menuMap.put("icon", menu.getIcon());
                List<Menu> childrenList = menu.getChildren();
                List<Map> childrenMapList = new ArrayList<>();
                if (childrenList != null && childrenList.size() > 0) {
                    for (Menu menu1 : childrenList) {
                        Map<String, Object> childrenMap = new HashMap<>();
                        childrenMap.put("path", menu1.getPath());
                        childrenMap.put("title", menu1.getName());
                        childrenMap.put("linkUrl", menu1.getLinkUrl());
                        childrenMapList.add(childrenMap);
                    }
                }
                menuMap.put("children", childrenMapList);
                listMap.add(menuMap);

            }
            map.put("username", user.getUsername());
            map.put("menu", listMap);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    //新增user
    @RequestMapping(value = "/add")
    public Result add(@RequestBody com.itheima.health.pojo.User user, @RequestParam(value = "roleIds") Integer[] roleIds) {
        try {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String password = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(password);
            userService.add(user,roleIds);
            return new Result(true, "新增用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增用户失败");
        }
    }
    //新增user
    @RequestMapping(value = "/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.findPage(queryPageBean.getQueryString(),
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize());
        return pageResult;
    }
    //通过userid查询user
    @RequestMapping(value = "/findById")
    public Result findById(Integer id) {
        com.itheima.health.pojo.User user = userService.findById(id);
        user.setPassword("*********");
        if(user!=null){
            return new Result(true,"查询用户成功",user);
        }
        return new Result(false,"查询用户失败");
    }
    //通过userid查询role
    @RequestMapping(value = "/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id) {
        try {
            List<Integer> list =  userService.findRoleIdsByUserId(id);
            return new Result(true,"",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"");
        }
    }
    //编辑
    @RequestMapping(value = "/editUser")
    public Result editUser(@RequestBody com.itheima.health.pojo.User user, @RequestParam(value = "roleIds") Integer [] roleIds) {
        try {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            userService.edit(user,roleIds);
            // 成功
            return new Result(true,"编辑用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, "编辑用户失败");
        }
    }
    //编辑
    @RequestMapping(value = "/deleteUser")
    public Result deleteUser(Integer id) {
        try {
            userService.deleteUser(id);
            return new Result(true,"删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
    }
    //编辑
    @RequestMapping(value = "/findAll")
    public Result findAll() {
        try {
            List<com.itheima.health.pojo.User> userList = userService.findAll();
            return new Result(true,"",userList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"");
        }
    }
}






